package com.lyl.security.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    public static <T> Result<T> build(BizCode bizCode) {
        return build(bizCode.getCode(), bizCode.getMsg(), null);
    }

    public static <T> Result<T> build(BizCode bizCode, T data) {
        return build(bizCode.getCode(), bizCode.getMsg(), data);
    }

    public static <T> Result<T> build(int code, String msg, T data) {
        return new Result<T>()
                .setCode(code)
                .setMsg(msg)
                .setData(data);
    }

    public static <T> Result<T> ok() {
        return build(ResultCode.SUCCESS, null);
    }

    public static <T> Result<T> ok(T data) {
        return build(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> fail() {
        return build(ResultCode.FAILURE, null);
    }

    public static <T> Result<T> fail(T data) {
        return build(ResultCode.FAILURE, data);
    }


}
